﻿using DadayunApi;
using DadayunApi.Model;
using KhtApi.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace KhtApi.DAL
{
    internal class FormDAL: KhtDAL
    {
        public FormDAL(AppAuthBase authBase, string baseUri) : base(authBase, baseUri)
        {          
           
        }
        /// <summary>
        /// 获取并过滤库存台账记录单
        /// </summary>
        /// <param name="filter">FormFilter过滤器</param>
        /// <returns></returns>
        internal List<InventoryBook> GetInventoryBooks(IEnumerable<FormFilter> filter)
        {
            FormTlpInfo tlpInfo = formApi.GetFormTemplate("库存台账记录单");
            List<InventoryBook> list = new List<InventoryBook>();
            List<FormInstanceInfo> forms = formApi.GetFormList(tlpInfo.Id, out int totalCount, 0, 0, "-CreateTime", filter, EKeyOptions.Entity);//设置最多读取全部数据
            if (forms.Count > 0)
            {
                foreach (var f in forms)
                {
                    string ans = formApi.GetFormInstance(tlpInfo.Id, f.Id, EKeyOptions.Entity);
                    list.Add(JsonConvert.DeserializeObject<InventoryBook>(ans));
                }
            }
            return list;
        }
        /// <summary>
        /// 根据表单过滤器获取产品清单
        /// </summary>
        /// <param name="filter">FormFilter过滤器</param>
        /// <returns></returns>
        internal List<ProductInfo> GetProductInfo(IEnumerable<FormFilter> filter)
        {
            FormTlpInfo tlpInfo = formApi.GetFormTemplate("产品信息表");//获取BOM单的模板信息
            List<ProductInfo> products = new List<ProductInfo>();
            List<FormInstanceInfo> forms = formApi.GetFormList(tlpInfo.Id, out int totalCount, 0, 0, "-CreateTime", filter, EKeyOptions.Entity);//设置最多读取全部数据
            if (forms.Count > 0)
            {
                foreach (var f in forms)
                {
                    string ans = formApi.GetFormInstance(tlpInfo.Id, f.Id, EKeyOptions.Entity);
                    products.Add(JsonConvert.DeserializeObject<ProductInfo>(ans));
                }
            }
            return products;
        }

        internal List<SaleOrder> GetSaleOrders(DateTime start, DateTime to)
        {
            FormTlpInfo tlpInfo = formApi.GetFormTemplate("销售订单");//获取指定表单模板信息
            List<SaleOrder> list = new List<SaleOrder>();
            IEnumerable<FormFilter> filter = new List<FormFilter>()
            {//过滤订单
                new FormFilter{Id="SoDate",Operator="range",Value1= start.ToShortDateString(),Value2 = to.ToShortDateString()}
            };
            List<FormInstanceInfo> forms = formApi.GetFormList(tlpInfo.Id, out int totalCount, 0, 0, "-CreateTime", filter, EKeyOptions.Entity);//设置最多读取全部数据
            if (forms.Count > 0)
            {
                foreach (var f in forms)
                {
                    string ans = formApi.GetFormInstance(tlpInfo.Id, f.Id, EKeyOptions.Entity);
                    list.Add(JsonConvert.DeserializeObject<SaleOrder>(ans));
                }
            }
            return list;
        }


        internal Bom GetBom(string instanceId)
        {
            FormTlpInfo tlpInfo = formApi.GetFormTemplate("BOM单");//获取BOM单的模板信息
            var form = formApi.GetFormInstance(tlpInfo.Id, instanceId, EKeyOptions.Entity);
            return JsonConvert.DeserializeObject<Bom>(form);
        }
        /// <summary>
        /// 获取所有BOM单，耗时，慎用！
        /// </summary>
        /// <returns></returns>
        public List<Bom> GetBoms()
        {
            FormTlpInfo tlpInfo = formApi.GetFormTemplate("BOM单");//获取BOM单的模板信息
            List<FormInstanceInfo> forms = formApi.GetFormList("BOM单", out int totalCount);
            List<Bom> bomList = new List<Bom>();
            foreach (var f in forms)
            {
                string ans = formApi.GetFormInstance(tlpInfo.Id, f.Id, EKeyOptions.Entity);
                Bom bom = JsonConvert.DeserializeObject<Bom>(ans);
                bomList.Add(bom);
            }
            return bomList;
        }
        /// <summary>
        /// 使用过滤器查询BOM单
        /// </summary>
        /// <param name="filter">表单过滤器</param>
        /// <returns></returns>
        public List<Bom> GetBoms(IEnumerable<FormFilter> filter)
        {
            FormTlpInfo tlpInfo = formApi.GetFormTemplate("BOM单");//获取BOM单的模板信息
            List<FormInstanceInfo> forms = formApi.GetFormList(tlpInfo.Id, out int totalCount, 0, 0, "-CreateTime", filter, EKeyOptions.Entity);//设置最多读取1000000条数据
            List<Bom> bomList = new List<Bom>();
            foreach (var f in forms)
            {
                string ans = formApi.GetFormInstance(tlpInfo.Id, f.Id, EKeyOptions.Entity);
                Bom bom = JsonConvert.DeserializeObject<Bom>(ans);
                bomList.Add(bom);
            }
            return bomList;
        }

        public bool UpdateBom(Bom bom)
        {
            FormTlpInfo tlpInfo = formApi.GetFormTemplate("BOM单");//获取BOM单的模板信息
            JsonSerializerSettings setting = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            };
            string bomJson = JsonConvert.SerializeObject(bom, setting);
            var ans = formApi.EditFormInstance(tlpInfo.Id, bom.Id, bomJson, true, EAutoFillMode.Null, EKeyOptions.Entity);
            return ans == System.Net.HttpStatusCode.OK;
        }


        internal List<Pmc> GetPmc(string soNo)
        {
            FormTlpInfo tlpInfo = formApi.GetFormTemplate("外挂PMC计划单");//获取指定表单模板信息
            List<Pmc> list = new List<Pmc>();
            IEnumerable<FormFilter> filter = new List<FormFilter>()
            {//过滤订单
                new FormFilter{Id="SoNo",Operator="like",Value1= soNo}
            };
            List<FormInstanceInfo> forms = formApi.GetFormList(tlpInfo.Id, out int totalCount, 0, 0, "-CreateTime", filter, EKeyOptions.Entity);
            if (forms.Count > 0)
            {
                foreach (var f in forms)
                {
                    string ans = formApi.GetFormInstance(tlpInfo.Id, f.Id, EKeyOptions.Entity);
                    list.Add(JsonConvert.DeserializeObject<Pmc>(ans));
                }
            }

            return list;
        }
        internal bool CreatePmc(Pmc pmc, out string instanceId)
        {
            FormTlpInfo tlpInfo = formApi.GetFormTemplate("外挂PMC计划单");//获取指定表单模板信息
            var ans = formApi.CreateFormInstance(tlpInfo.Id, JsonConvert.SerializeObject(pmc), out instanceId, true, EAutoFillMode.ForceAll, EKeyOptions.Entity);
            return !string.IsNullOrEmpty(instanceId);
        }
    }
}
